#include "event_mapping.h"
#include <map.h>
#include <xml.h>
#include <stdlib.h>
#include <functional.h>
#include <windows.h>

smap *mapping = NULL;

int event_map_load()
{
    xml *x;
    xml *evt;
    mapping = im_create();
    x = xml_parse( "frame.xml" );
    if( x != NULL )
    {
        evt = xml_find( x, "mapping" );
        while( evt )
        {
            HMODULE handle;
            map_info *info;
            event_map *em;
            int64 id;

            info = (map_info *)malloc( sizeof(map_info) );
            str_cpy( &info->lib_path, xml_attr( evt, "library" ) );
            str_cpy( &info->func_name, xml_attr( evt, "function" ) );
            str_cpy( &info->version, xml_attr( evt, "version" ) );
            str_cpy( &info->desc, xml_attr( evt, "desc" ) );

            em = (event_map *)malloc( sizeof(event_map) );
            em->info = info;
            handle = LoadLibrary( info->lib_path );
            em->func = GetProcAddress( handle, info->func_name );
            id = _atoi64( xml_attr( evt, "id" ) );

            im_insert( mapping, id, em );

            evt = xml_brother( evt );
        }
    }
    return 0;
}

event_map* get_processor( int64 evt )
{
    event_map *em = NULL;
    im_find( mapping, evt, (void**)&em );
    return em;
}

